//
//  OCProfileViewController.m
//  League
//
//  Created by JackChen on 2018/2/19.
//  Copyright © 2018年 ciyuanjuzhen. All rights reserved.
//

#import "OCProfileViewController.h"
#import "OCPreferencesViewController.h"
#import "gkhPickerImage.h"
#import "UserImageModel.h"
#import "UserInfomation.h"
#import "ChoseCityViewController.h"
#import "PickerListViewController.h"
#import "ProfileManager.h"
#import "AddInterestAlertView.h"
#import "ProdileModel.h"
#import "MyProfileModel.h"

#import "SegmentCell.h"
#import "TextFieldCell.h"
#import "SliderTableViewCell.h"
#import "ProfilePhotoCell.h"
#import "ProfileSelectCell.h"
#import "ProfileAddCell.h"
#import "ProfileDoneCell.h"
#import "PreferencesRangeSliderCell.h"

@interface OCProfileViewController ()<UITableViewDataSource, UITableViewDelegate>
{
    NSInteger selectIndex;
    NSInteger selectCityIndex; //城市
}

@property (strong, nonatomic) UITableView *tableView;
@property (strong , nonatomic) NSMutableArray *imageArr;
@property (strong , nonatomic) NSMutableArray *cityList;
@property(strong,nonatomic) UIAlertView *imgUpAlert;
@property (strong, nonatomic) ProfileManager *manager;
@property(strong , nonatomic) AddInterestAlertView *addInterestAlt;

@end

@implementation OCProfileViewController
static NSString *const cellIdentity_segment = @"segmentCell";
static NSString *const cellIdentity_textField = @"textField";
static NSString *const cellIdentity_slider = @"slider";
static NSString *const cellIdentity_photo = @"photo";
static NSString *const cellIdentity_select = @"select";
static NSString *const cellIdentity_add = @"add";
static NSString *const cellIdentity_done = @"done";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    self.title = [self.languageBundle localizedStringForKey:@"EditProfile" value:nil table:@"hello"];
    //init property
    if (!_imageArr) {
        self.imageArr = [NSMutableArray array];
    }
    self.cityList = [NSMutableArray array];
    self.manager = [ProfileManager shareManager];
    if (self.manager.religionsArray == nil) {
        self.manager.religionsArray = [NSMutableArray array];
    }
    
    [self getCities];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

- (void)getCities {
    NSDictionary *dic = @{
                          @"parentid":@(0),
                          @"typename":@"LocationCity"
                          };
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",kFirstUrl,kGetDictionary];
    NSLog(@"urlStr:%@",urlStr);
    [DataContrller PostRequestUrl:urlStr Param:dic requestSuccess:^(NSDictionary *backDic) {
        NSLog(@"backDic:%@",backDic);
        if ([backDic[@"returnCode"] integerValue] == 200) {
            for (NSDictionary *city in backDic[@"obj"]) {
                ProdileModel *model = [[ProdileModel alloc] initWithDictionary:city error:nil];
                if (model != nil) {
                    [self.cityList addObject:model];
                }
                if (self.cityList.count > 0) {
                    //第一个默认选中
                    ProdileModel *defaultmodel = self.cityList[0];
                    self.manager.location = defaultmodel._id;
                }
                if (self.isRegister == NO) {
                    //不是注册
                    [self getUserInfoReqeust];
                }
            }
            [self.tableView reloadData];
        } else {
            
        }
    } requestFail:^(NSString *backErrorStr) {
        NSLog(@"backErrorStr:%@",backErrorStr);
    }];
}

- (void)getUserInfoReqeust {
    NSDictionary *dic = @{
                          @"pk1":[UserInfomation shareManager].pk
                          };
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",kFirstUrl,kProfileInfo];
    NSLog(@"urlStr:%@",urlStr);
    [DataContrller PostRequestUrl:urlStr Param:dic requestSuccess:^(NSDictionary *backDic) {
        NSLog(@"backDic:%@",backDic);
        if ([backDic[@"returnCode"] integerValue] == 200) {
            MyProfileModel *model = [[MyProfileModel alloc] initWithDictionary:backDic[@"obj"] error:nil];
            self.manager.sex = model.sex;
            if (model.userName != nil) {
                self.manager.username = model.userName;
            }
            if (model.email != nil) {
                self.manager.email = model.email;
            }
            self.manager.age = model.age;
            [self.manager.photoIds removeAllObjects];
            if (self.manager.photoIds == nil) {
                self.manager.photoIds = [NSMutableArray array];
            }
            if (self.imageArr == nil) {
                self.imageArr = [NSMutableArray array];
            }
            [self.imageArr removeAllObjects];
            for (CustomerPhotosModel *photoModel in model.customerPhotos) {
                if (photoModel._id != nil && photoModel.picUrl != nil) {
                    [self.manager.photoIds addObject:photoModel._id];
                    UserImageModel *imageModel = [[UserImageModel alloc] init];
                    imageModel.picUrl = photoModel.picUrl;
                    imageModel.imageId = photoModel._id;
                    [self.imageArr addObject:imageModel];
                }
            }
            if (model.aboutme != nil) {
                self.manager.aboutme = model.aboutme;
            }
            self.manager.single = model.single;
            self.manager.height = model.height;
            if (model.locationId != nil) {
                self.manager.location = model.locationId;
            }
            //宗教
            self.manager.religionsArray = [NSMutableArray arrayWithArray:model.religions];
            //职业
            [self.manager.professionsArray removeAllObjects];
            if (self.manager.professionsArray == nil) {
                self.manager.professionsArray = [NSMutableArray array];
            }
            for (CustomerProfessionsModel *profrssionModel in model.customerProfessions) {
                if (profrssionModel._id != nil && profrssionModel.profession != nil) {
                    [self.manager.professionsArray addObject:profrssionModel.profession];
                }
            }
            //教育
            [self.manager.educationsArray removeAllObjects];
            if (self.manager.educationsArray == nil) {
                self.manager.educationsArray = [NSMutableArray array];
            }
            for (CustomerEducationsModel *educationsModel in model.customerEducations) {
                if (educationsModel._id != nil && educationsModel.education != nil) {
                    [self.manager.educationsArray addObject:educationsModel.education];
                }
            }
            //兴趣
            [self.manager.interestsArray removeAllObjects];
            if (self.manager.interestsArray == nil) {
                self.manager.interestsArray = [NSMutableArray array];
            }
            for (CustomerInterestsModel *inserstingModel in model.customerInterests) {
                if (inserstingModel._id != nil && inserstingModel.interest != nil) {
                    [self.manager.interestsArray addObject:inserstingModel.interest];
                }
            }
            [self.tableView reloadData];
        } else {
            
        }
    } requestFail:^(NSString *backErrorStr) {
        
    }];
}

- (void)editProfileRequest {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary *parameters = [self setParameters];
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",kFirstUrl,kEditProfileInfo];
    NSLog(@"urlStr:%@",urlStr);
    [DataContrller PostRequestUrl:urlStr Param:parameters requestSuccess:^(NSDictionary *backDic) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSLog(@"backDic:%@",backDic);
        if ([backDic[@"returnCode"] integerValue] == 200) {
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [self showErrorsWithMessage:backDic];
        }
    } requestFail:^(NSString *backErrorStr) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSLog(@"backErrorStr:%@",backErrorStr);
        [self showAlert:backErrorStr];
        
    }];
}

- (NSDictionary *)setParameters {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:[UserInfomation shareManager].pk forKey:@"pk"];
    [parameters setObject:@(self.manager.sex) forKey:@"sex"];
    if (self.manager.username != nil) {
        [parameters setObject:self.manager.username forKey:@"username"];
        [parameters setObject:self.manager.username forKey:@"realname"];
    }
    if (self.manager.email != nil) {
        [parameters setObject:self.manager.email forKey:@"email"];
    }
    [parameters setObject:@(self.manager.age) forKey:@"age"];
    if (self.manager.aboutme != nil) {
        [parameters setObject:self.manager.aboutme forKey:@"aboutme"];
    }
    [parameters setObject:@(self.manager.sex) forKey:@"sex"];
    [parameters setObject:@(self.manager.height) forKey:@"height"];
    [parameters setObject:@(self.manager.single) forKey:@"single"];
    //宗教
    if (self.manager.religionsArray.count != 0) {
        NSMutableString *religionsString = [NSMutableString string];
        for (int i=0; i<self.manager.religionsArray.count; i++) {
            if (i == self.manager.religionsArray.count - 1) {
                [religionsString appendString:[NSString stringWithFormat:@"%@", self.manager.religionsArray[i]]];
            } else {
                [religionsString appendString:[NSString stringWithFormat:@"%@;", self.manager.religionsArray[i]]];
            }
        }
        if (religionsString != nil) {
            [parameters setObject:religionsString forKey:@"religion"];
        }
    }
    //职业
    if (self.manager.professionsArray.count != 0) {
        NSMutableString *professionsString = [NSMutableString string];
        for (int i=0; i<self.manager.professionsArray.count; i++) {
            if (i == self.manager.professionsArray.count - 1) {
                [professionsString appendString:[NSString stringWithFormat:@"%@", self.manager.professionsArray[i]]];
            } else {
                [professionsString appendString:[NSString stringWithFormat:@"%@;", self.manager.professionsArray[i]]];
            }
        }
        if (professionsString != nil) {
            [parameters setObject:professionsString forKey:@"professions"];
        }
    }
    //教育
    if (self.manager.educationsArray.count != 0) {
        NSMutableString *educationsString = [NSMutableString string];
        for (int i=0; i<self.manager.educationsArray.count; i++) {
            if (i == self.manager.educationsArray.count - 1) {
                [educationsString appendString:[NSString stringWithFormat:@"%@", self.manager.educationsArray[i]]];
            } else {
                [educationsString appendString:[NSString stringWithFormat:@"%@;", self.manager.educationsArray[i]]];
            }
        }
        if (educationsString != nil) {
            [parameters setObject:educationsString forKey:@"educations"];
        }
    }
    //兴趣
    if (self.manager.interestsArray.count != 0) {
        NSMutableString *interestsString = [NSMutableString string];
        for (int i=0; i<self.manager.interestsArray.count; i++) {
            if (i == self.manager.interestsArray.count - 1) {
                [interestsString appendString:[NSString stringWithFormat:@"%@", self.manager.interestsArray[i]]];
            } else {
                [interestsString appendString:[NSString stringWithFormat:@"%@;", self.manager.interestsArray[i]]];
            }
        }
        if (interestsString != nil) {
            [parameters setObject:interestsString forKey:@"interests"];
        }
    }
    if (self.manager.location != nil) {
        [parameters setObject:self.manager.location forKey:@"location"];
    }
    NSString *lan = [InternationalControl userLanguage];
    if([lan isEqualToString:@"en"]){//判断当前的语言，进行改变
        [parameters setObject:@(1) forKey:@"systemLanguage"];
    } else {
        [parameters setObject:@(0) forKey:@"systemLanguage"];
    }
    return parameters;
}

- (void)nextAction:(UIButton *)sender {
    if (self.imageArr.count < 5) {
        [self showErrorsWithMessage:@"请至少上传五张图片"];
        return;
    }
    if (self.manager.photoIds == nil) {
        self.manager.photoIds = [NSMutableArray array];
    }
    [self.manager.photoIds removeAllObjects];
    if (self.imageArr.count > 0) {
        for (UserImageModel *model in self.imageArr) {
            [self.manager.photoIds addObject:model.imageId];
        }
    }
    if (self.isRegister == NO) {
        [self editProfileRequest];
        return;
    }
    OCPreferencesViewController *vc = [[OCPreferencesViewController alloc] init];
    vc.isRegister = self.isRegister;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)choseCity:(UIButton *)sender {
    if (sender.tag - 300 == 8) {
        ChoseCityViewController *vc = [self  initializeStoryboardViewControllerWithStoryboardID:@"LoginRegister" viewcontroller:@"ChoseCityViewController"];
        vc.dataSource = self.cityList;
        vc.isProfile = YES;
        vc.selectBlock = ^{
            [self.tableView reloadData];
        };
        vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        vc.view.backgroundColor = [UIColor colorWithWhite:.0f alpha:0.6f];
        [self.navigationController presentViewController:vc animated:NO completion:nil];
    } else {
        PickerListViewController *pickerVc = [[PickerListViewController alloc] init];
        pickerVc.isProfile = YES;
        pickerVc.index = sender.tag;
        pickerVc.type = Religion;
        [self presentViewController:pickerVc animated:YES completion:nil];
    }
}

-(void)addInster:(NSInteger)index
{
    NSBundle *bundle = self.languageBundle;
    [self.addInterestAlt removeFromSuperview];
    self.addInterestAlt = [[AddInterestAlertView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    [[UIApplication sharedApplication].keyWindow addSubview:self.addInterestAlt];
    if (index == 10) {
        self.addInterestAlt.headLab.text = [bundle localizedStringForKey:@"AddWork" value:nil table:@"hello"];
    } else if (index == 11) {
        self.addInterestAlt.headLab.text = [bundle localizedStringForKey:@"AddEducation" value:nil table:@"hello"];
    } else {
        self.addInterestAlt.headLab.text = [bundle localizedStringForKey:@"addinterest" value:nil table:@"hello"];
    }
    [self.addInterestAlt.addBtn setTitle:[bundle localizedStringForKey:@"Add" value:nil table:@"hello"] forState:UIControlStateNormal];
    [self.addInterestAlt.addBtn addTarget:self action:@selector(AddAction:) forControlEvents:UIControlEventTouchUpInside];
    self.addInterestAlt.addBtn.tag = 400 + index;
    [self.addInterestAlt.cancelBtn setTitle:[bundle localizedStringForKey:@"cancel" value:nil table:@"hello"] forState:UIControlStateNormal];
}

- (void)removeprofession:(NSInteger)index {
    [self.manager.professionsArray removeObjectAtIndex:index];
    [self.tableView reloadData];
}

- (void)removeducation:(NSInteger)index {
    [self.manager.educationsArray removeObjectAtIndex:index];
    [self.tableView reloadData];
}

-(void)removeinterest:(NSInteger)index
{
    [self.manager.interestsArray removeObjectAtIndex:index];
    [self.tableView reloadData];
}

- (void)AddAction:(UIButton *)sender {
    [self.addInterestAlt removeFromSuperview];
    NSInteger index = sender.tag - 400;
    if (index == 10) {
        if (self.manager.professionsArray == nil) {
            self.manager.professionsArray = [NSMutableArray array];
        }
        [self.manager.professionsArray addObject:self.addInterestAlt.textFileOne.text];
    } else if (index == 11) {
        if (self.manager.educationsArray == nil) {
            self.manager.educationsArray = [NSMutableArray array];
        }
        [self.manager.educationsArray addObject:self.addInterestAlt.textFileOne.text];
    } else {
        if (self.manager.interestsArray == nil) {
            self.manager.interestsArray = [NSMutableArray array];
        }
        [self.manager.interestsArray addObject:self.addInterestAlt.textFileOne.text];
    }
    [self.tableView reloadData];
}

- (void)postImage:(UIImage *)image :(NSString *)type :(NSString *)name
{
    NSLog(@"正在上传图片");
    NSLog(@"照片名:%@",name);
    NSString *msg = [NSString stringWithFormat:@"%@上传中...",name];
    self.imgUpAlert = [[UIAlertView alloc]initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [self.imgUpAlert show];
    NSString *a = [NSString stringWithFormat:@"%@%@",kFirstUrl,kUpImageUrl];
    NSDictionary *params;
    if (self.isRegister == NO) {
        if (selectIndex < self.imageArr.count - 1) {
            UserImageModel *model = self.imageArr[selectIndex];
            params = @{@"customerId":[UserInfomation shareManager].pk,@"sortOrder":[NSString stringWithFormat:@"%@",@(selectIndex)],@"pk":model.imageId};
        } else {
            params = @{@"customerId":[UserInfomation shareManager].pk,@"sortOrder":[NSString stringWithFormat:@"%@",@(selectIndex)],@"pk":@"0"};
        }
    } else {
        params = @{@"customerId":@"0",@"sortOrder":[NSString stringWithFormat:@"%@",@(selectIndex)],@"pk":@"0"};
    }
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:a parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
//        NSData *data = UIImagePNGRepresentation(image);
        NSData *data = [self imageData:image];
        NSString *fileName = @"12312312.jpg";
        [formData appendPartWithFileData:data name:@"myfile" fileName:fileName mimeType:@"image/png"];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        NSLog(@"%f",1.0 * uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
        dispatch_async(dispatch_get_main_queue(), ^{
        });
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"responseObject:%@",responseObject);
        [self.imgUpAlert dismissWithClickedButtonIndex:0 animated:NO];
        UserImageModel *model = [[UserImageModel alloc] init];
        model.imageId = responseObject[@"obj"][@"pk"];
        model.picUrl = responseObject[@"obj"][@"url"];
        if ([type isEqualToString:@"0"]) {
            [self.imageArr replaceObjectAtIndex:selectIndex withObject:model];
        } else {
            [self.imageArr addObject:model];
        }
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",[error localizedDescription]);
        [self.imgUpAlert dismissWithClickedButtonIndex:0 animated:NO];
    }];
    
}

- (NSData *)imageData:(UIImage *)myimage
{
    NSData *data=UIImageJPEGRepresentation(myimage, 1.0);
    if (data.length>100*1024) {
        if (data.length>1024*1024) {//1M以及以上
            data=UIImageJPEGRepresentation(myimage, 0.1);
        }else if (data.length>512*1024) {//0.5M-1M
            data=UIImageJPEGRepresentation(myimage, 0.5);
        }else if (data.length>200*1024) {//0.25M-0.5M
            data=UIImageJPEGRepresentation(myimage, 0.9);
        }
    }
    return data;
}

#pragma mark -- tableView delegate and dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 14;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSBundle *bundle = self.languageBundle;
    if (indexPath.row == 0 || indexPath.row == 6) {
        SegmentCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentity_segment];
        if (cell == nil) {
            cell = [[SegmentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentity_segment];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSString *titleString;
        NSArray *stateArray;
        if (indexPath.row == 0) {
            titleString = [bundle localizedStringForKey:@"Sex" value:nil table:@"hello"];
            stateArray = [NSArray arrayWithObjects:[bundle localizedStringForKey:@"Boy" value:nil table:@"hello"],[bundle localizedStringForKey:@"Girl" value:nil table:@"hello"], nil];
            cell.state = ProfileSexState;
        }
        if (indexPath.row == 6) {
            titleString = [bundle localizedStringForKey:@"Status" value:nil table:@"hello"];
            stateArray = [NSArray arrayWithObjects:[bundle localizedStringForKey:@"Single" value:nil table:@"hello"],[bundle localizedStringForKey:@"NotSingle" value:nil table:@"hello"], nil];
            cell.state = ProfileSigleState;
        }
        [cell bindViewWithTitle:titleString segmentState:stateArray];
        if (indexPath.row == 0) {
            [cell.stateSegment setSelectedSegmentIndex:self.manager.sex];
        }
        if (indexPath.row == 6) {
            [cell.stateSegment setSelectedSegmentIndex:self.manager.single];
        }
        return cell;
    }
    if (indexPath.row == 1 || indexPath.row == 2 || indexPath.row == 5) {
        TextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentity_textField];
        if (cell == nil) {
            cell = [[TextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentity_textField];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSString *titleString;
        if (indexPath.row == 1) {
            titleString = [bundle localizedStringForKey:@"cinUserName" value:nil table:@"hello"];
            cell.state = ProfileUserNameState;
            if (self.manager.username != nil) {
                cell.contentTextField.text = self.manager.username;
            }
        }
        if (indexPath.row == 2) {
            titleString = [bundle localizedStringForKey:@"Email" value:nil table:@"hello"];
            cell.state = ProfileEmailState;
            if (self.manager.email != nil) {
                cell.contentTextField.text = self.manager.email;
            }
        }
        if (indexPath.row == 5) {
            titleString = [bundle localizedStringForKey:@"AboutMe" value:nil table:@"hello"];
            cell.state = ProfileAboutMeState;
            if (self.manager.aboutme != nil) {
                cell.contentTextField.text = self.manager.aboutme;
            }
        }
        cell.titleLabel.text = titleString;
        return cell;
    }
    if (indexPath.row == 3 || indexPath.row == 7) {
        PreferencesRangeSliderCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentity_slider];
        if (cell == nil) {
            cell = [[PreferencesRangeSliderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentity_slider];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSString *titleString;
        if (indexPath.row == 3) {
            titleString = [bundle localizedStringForKey:@"Age" value:nil table:@"hello"];
            cell.min = 1;
            cell.max = 100;
            if (self.manager.age == 0) {
                cell.selectmax = 20;
            } else {
                cell.selectmax = self.manager.age;
            }
            [cell setSelectNum];
        }
        if (indexPath.row == 7) {
            titleString = [bundle localizedStringForKey:@"Height" value:nil table:@"hello"];
            cell.min = 100;
            cell.max = 220;
            if (self.manager.height == 0) {
                cell.selectmax = 160;
            } else {
                cell.selectmax = self.manager.height;
            }
            [cell setSelectNum];
        }
        cell.isProfile = YES;
        cell.titleLabel.text = titleString;
        cell.rangeSlider.leftHandle.hidden = YES;
        cell.rangeSlider.minLabel.hidden = YES;
        return cell;
    }
    if (indexPath.row == 4) {
        ProfilePhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentity_photo];
        if (cell == nil) {
            cell = [[ProfilePhotoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentity_photo];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.imageArr = self.imageArr;
        [cell.collection reloadData];
        cell.titleLabel.text = [bundle localizedStringForKey:@"Photos" value:nil table:@"hello"];
        cell.chosePhoto = ^(NSInteger index) {
            selectIndex = index;
            if (indexPath.row < self.imageArr.count - 1) {
                [gkhPickerImage sharePictureByController:self susscond:^(UIImage *image, UIViewController *controller) {
                    [self postImage:image :@"0" :@""];
                }];
            } else {
                [gkhPickerImage sharePictureByController:self susscond:^(UIImage *image, UIViewController *controller) {
                    [self postImage:image :@"1":@""];
                }];
            }
        };
        return cell;
    }
    if (indexPath.row == 8 || indexPath.row == 9) {
        ProfileSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentity_select];
        if (cell == nil) {
            cell = [[ProfileSelectCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentity_select];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSString *titleString;
        if (indexPath.row == 8) {
            titleString = [bundle localizedStringForKey:@"Distance" value:nil table:@"hello"];
            if (self.cityList.count > 0) {
                for (int i = 0; i<self.cityList.count; i++) {
                    ProdileModel *model = self.cityList[i];
                    if ([model._id isEqualToString:[ProfileManager shareManager].location]) {
                        NSString *lan = [InternationalControl userLanguage];
                        if([lan isEqualToString:@"en"]){//判断当前的语言，进行改变
                            [cell.locationButton setTitle:model.categoryDesc forState:UIControlStateNormal];
                        } else {
                            [cell.locationButton setTitle:model.categoryName forState:UIControlStateNormal];
                        }
                    }
                }
            }
        }
        if (indexPath.row == 9) {
            titleString = [bundle localizedStringForKey:@"Religion" value:nil table:@"hello"];
            if (self.manager.religionsArray.count == 0) {
                [cell.locationButton setTitle:[bundle localizedStringForKey:@"NoPreference" value:nil table:@"hello"] forState:UIControlStateNormal];
            } else {
                [cell.locationButton setTitle:[NSString stringWithFormat:@"%ld religions selected", self.manager.religionsArray.count] forState:UIControlStateNormal];
            }
        }
        cell.titleLabel.text = titleString;
        cell.locationButton.tag = 300 + indexPath.row;
        [cell.locationButton addTarget:self action:@selector(choseCity:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
    if (indexPath.row == 10 || indexPath.row == 11 || indexPath.row == 12) {
        ProfileAddCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentity_add];
        if (cell == nil) {
            cell = [[ProfileAddCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentity_add];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSString *titleString;
        if (indexPath.row == 10) {
            titleString = [bundle localizedStringForKey:@"Profession" value:nil table:@"hello"];
            cell.dataArray = self.manager.professionsArray;
            cell.deleteBlock = ^(NSInteger index) {
                [self removeprofession:index];
            };
        }
        if (indexPath.row == 11) {
            titleString = [bundle localizedStringForKey:@"Education" value:nil table:@"hello"];
            cell.dataArray = self.manager.educationsArray;
            cell.deleteBlock = ^(NSInteger index) {
                [self removeducation:index];
            };
        }
        if (indexPath.row == 12) {
            titleString = [bundle localizedStringForKey:@"Interests" value:nil table:@"hello"];
            cell.dataArray = self.manager.interestsArray;
            cell.deleteBlock = ^(NSInteger index) {
                [self removeinterest:index];
            };
        }
        cell.addBlock = ^(NSInteger index) {
            [self addInster:indexPath.row];
        };
        cell.titleLabel.text = titleString;
        [cell addContent];
        return cell;
    }
    if (indexPath.row == 13) {
        ProfileDoneCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentity_done];
        if (cell == nil) {
            cell = [[ProfileDoneCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentity_done];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.doneButton addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
        if (self.isRegister == YES) {
            [cell.doneButton setTitle:[bundle localizedStringForKey:@"Next" value:nil table:@"hello"] forState:UIControlStateNormal];
        } else {
            [cell.doneButton setTitle:[bundle localizedStringForKey:@"over" value:nil table:@"hello"] forState:UIControlStateNormal];
        }
        return cell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0 || indexPath.row == 6) {
        return 50;
    }
    if (indexPath.row == 1 || indexPath.row == 2 || indexPath.row == 5) {
        return 70;
    }
    if (indexPath.row == 3 || indexPath.row == 7) {
        return 80;
    }
    if (indexPath.row == 4) {
        return 20 + (kScreenWidth - 100)/3.0 * 2;
    }
    if (indexPath.row == 8 || indexPath.row == 9) {
        return 70;
    }
    if (indexPath.row == 10) {
        NSInteger count = self.manager.professionsArray.count == 0 ? 1 : self.manager.professionsArray.count;
        return 34 * count ;
    }
    if (indexPath.row == 11) {
        NSInteger count = self.manager.educationsArray.count == 0 ? 1 : self.manager.educationsArray.count;
        return 34 * count ;
    }
    if (indexPath.row == 12) {
        NSInteger count = self.manager.interestsArray.count == 0 ? 1 : self.manager.interestsArray.count;
        return 34 * count ;
    }
    if (indexPath.row == 13) {
        return 64;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    view.backgroundColor = [UIColor whiteColor];
    return view;
}

#pragma mark setter and getter
- (UITableView *)tableView {
    if (_tableView) {
        return _tableView;
    }
    if (self.isRegister == NO) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, kScreenHeight - 64)];
    } else {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, kScreenHeight)];
    }
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.estimatedRowHeight = 0;
    _tableView.estimatedSectionHeaderHeight = 0;
    _tableView.estimatedSectionFooterHeight = 0;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //注册cell
    UINib *nib_segment = [UINib nibWithNibName:@"SegmentCell" bundle:nil];
    [_tableView registerNib:nib_segment forCellReuseIdentifier:cellIdentity_segment];
    UINib *nib_textField = [UINib nibWithNibName:@"TextFieldCell" bundle:nil];
    [_tableView registerNib:nib_textField forCellReuseIdentifier:cellIdentity_textField];
    UINib *nib_slider = [UINib nibWithNibName:@"PreferencesRangeSliderCell" bundle:nil];
    [_tableView registerNib:nib_slider forCellReuseIdentifier:cellIdentity_slider];
    UINib *nib_photo = [UINib nibWithNibName:@"ProfilePhotoCell" bundle:nil];
    [_tableView registerNib:nib_photo forCellReuseIdentifier:cellIdentity_photo];
    UINib *nib_select = [UINib nibWithNibName:@"ProfileSelectCell" bundle:nil];
    [_tableView registerNib:nib_select forCellReuseIdentifier:cellIdentity_select];
    UINib *nib_add = [UINib nibWithNibName:@"ProfileAddCell" bundle:nil];
    [_tableView registerNib:nib_add forCellReuseIdentifier:cellIdentity_add];
    UINib *nib_done = [UINib nibWithNibName:@"ProfileDoneCell" bundle:nil];
    [_tableView registerNib:nib_done forCellReuseIdentifier:cellIdentity_done];
    return _tableView;
}

@end
